EstimateRequiredPurchases <- function(Conf,Cost,RiskTable) {#RLA_Rate,BNcK_Rate,RecertSchedule,RecertRate,RiskLimits) {
  
  # This is the "Reverse Function" designed to give a PM 
  # the power to ask, "What do I need to chieve this result?"
  #
  # RiskLimits - 2x2 - [xmin ymin;
  #                     xmax ymax]
  #===========================================================================
  
  CostLimits = c(min(RiskTable$CostLimits[,1]),max(RiskTable$CostLimits[,2]))
  ConfLimits = c(min(RiskTable$ConfLimits[,1]),max(RiskTable$ConfLimits[,2]))
  MinLimits = c(CostLimits[1],ConfLimits[1])
  MaxLimits = c(CostLimits[2],ConfLimits[2])
  
  CostRange = (CostLimits[2] - CostLimits[1])
  ConfRange = (ConfLimits[2] - ConfLimits[1])
  RiskRange = c(CostRange,ConfRange)
  GivenRisk = c(Cost,Conf)
  GivenRiskPcnt = (GivenRisk -MinLimits) / RiskRange
  
  dimTable = dim(RiskTable$RiskCost)
  RiskNorm = 10000000
  for (a in 1:dimTable[1]) {
    for (b in 1:dimTable[2]) {
      for (c in 1:dimTable[3]) {
        
        testRisk = c(RiskTable$RiskCost[a,b,c],
                     RiskTable$RiskConf[a,b,c])
        testRiskPcnt = (testRisk -MinLimits) / RiskRange
        delta = (GivenRiskPcnt - testRiskPcnt)
        
        deltaNorm = sqrt(sum(delta^2))
        if (deltaNorm < RiskNorm) {
          RiskNorm = deltaNorm
          index = c(a,b,c)
        }
      }
    }
  }
  RiskFound = list("Scale"=RiskTable$Scale[index[1]],
                   "Alpha"=RiskTable$Alpha[index[2]],
                   "RecertRate"=RiskTable$RecertRate[index[3]],
                   "Index"=index)
  return(RiskFound)
}
#===========================================================================
CalcS_GivenD <- function(Conf,D) { 
  # Usually determined by Demand and a given Stock,
  # Now determined by Demand and a Given Confidence
  # I would really like a closed for solution for this, but brute force will work for now
  # ... It almost HURTS to code this!!
  bestS = FALSE
  lastConf = 0
  lastDiff = 100 # Should be between 0-1, but 100 just in case
  S = 0
  count = 0
  while (!bestS) {
    count = count +1
    thisConf = sum( exp(-D)*D^(0:S)/factorial(0:S) )
    thisDiff = abs(Conf-thisConf)
    if (thisDiff < lastDiff) {
      lastDiff = thisDiff
      S = S +1
    } else {
      bestS = TRUE
      return(S)
    }
    if (count >20) {
      error("We are not reaching the Supply we expect!")
    }
  }
  
}